package com.keba.lamp.oauth.controller;


import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.keba.basic.base.R;
import com.keba.basic.context.ContextUtil;
import com.keba.basic.exception.BizException;
import com.keba.basic.model.log.OptLogDTO;
import com.keba.basic.utils.BeanPlusUtil;
import com.keba.lamp.base.service.system.BaseOperationLogService;
import com.keba.lamp.base.vo.save.system.BaseOperationLogSaveVO;
import com.keba.lamp.oauth.granter.CaptchaTokenGranter;

/**
 * <p>
 * 前端控制器
 * 系统日志
 * </p>
 *
 * @author zuihou
 * @date 2019-07-22
 */
@Slf4j
@Validated
@RestController
@Tag(name = "系统访问日志")
@RequiredArgsConstructor
@Hidden
public class WebLogController {

    private final BaseOperationLogService baseOperationLogService;

    /**
     * 保存系统日志
     *
     * @param data 保存对象
     * @return 保存结果
     */
    @PostMapping("/optLog")
    @Operation(summary = "保存系统日志", description = "保存系统日志不为空的字段")
    public R<Boolean> save(@RequestBody OptLogDTO data) {
        if (!ContextUtil.isEmptyTenantId()) {
            baseOperationLogService.save(BeanPlusUtil.toBean(data, BaseOperationLogSaveVO.class));
        }
        return R.success(true);
    }


    @Value("${test1:def}")
    private String test1;
    @Value("${test2:def}")
    private String test2;

    @PostMapping("/anyTenant/enums/test1")
    public void test1() {
        log.info("1234={}-===_{}", test1, test2);
    }


    @PostMapping(value = "/enums/test2")
    public R<String> test2() {
        log.info("1234");
        return R.success("1234");
    }

    @GetMapping(value = "/checkCaptcha2")
    public R<Boolean> checkCaptcha2(@RequestParam(value = "key") String key, @RequestParam(value = "code") String code,
                                    @RequestParam(value = "templateCode", required = false, defaultValue = CaptchaTokenGranter.GRANT_TYPE)
                                    String templateCode) throws BizException {
        return R.success();
    }
}
